﻿/****************
* Authored by Zhuo "Wizcas" Chen
* 2018 © Wubi.fun all rights reserved
*****************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

[RequireComponent(typeof(UI3DRenderer))]
public class UIBlock : MonoBehaviour , IPointerDownHandler
{
    public bool interactable;
    UI3DRenderer _r3d;
    UI3DRenderer render3D { get { return _r3d ?? (_r3d = GetComponent<UI3DRenderer>()); } }

    public Block block
    {
        get { return render3D.runtimeTargetObj.GetComponent<Block>(); }
        set { SetRender3D(value); }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if (!interactable) return;
        var b = block;
        BlockInput.instance.SetDrag(b);
        if (b != null) b.onSettled += PlaceBlock;
    }

    void SetRender3D(Block block)
    {
        render3D.runtimeTargetObj = block == null ? null : block.transform;
        if (block != null) render3D.camSettings.offset.y = block.height * .5f;
    }

    void PlaceBlock(Block block)
    {
        BlockQueue.instance.Dequeue(block);
        block.onSettled -= PlaceBlock;
    }
}
